package com.lyf.shop.web.controller.admin;

import com.lyf.shop.common.Const;
import com.lyf.shop.entity.Product;
import com.lyf.shop.entity.Type;
import com.lyf.shop.services.Impl.ProductServiceImpl;
import com.lyf.shop.services.Impl.TypeServiceImpl;
import com.lyf.shop.services.ProductService;
import com.lyf.shop.services.TypeService;
import com.lyf.shop.web.controller.BaseController;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@WebServlet("/admin_pro")
@MultipartConfig(maxFileSize = 1024*100*100,maxRequestSize = 1024*100*100)
public class AdminProductController extends BaseController {
    private TypeService typeService = new TypeServiceImpl();
    private ProductService productService = new ProductServiceImpl();

    /**
     * 新增商品信息
     * @param request
     * @param response
     * @return
     */
    public String addPro(HttpServletRequest request,HttpServletResponse response){
        //先获取普通数据
        Map<String, String[]> parameterMap = request.getParameterMap();
        //声明一个封装的实体类
        Product p = new Product();
        try {
            BeanUtils.populate(p,parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //文件上传
        //获取上传的文件
        Part part = null;
        try {
            part = request.getPart("pimage");
            if (part!=null){
                String fileName = part.getSubmittedFileName();//获取文件名
                //获取项目真实路径
                String realPath = request.getServletContext().getRealPath("/image");
                //封装到product对象==>保存图片路径到数据库
                p.setPimage("image/"+fileName);
                //把图片写出到我们的图片文件夹下面去
                part.write(realPath+"\\"+fileName);
            }
            //访问业务层保存数据到数据库
            productService.addPro(p);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ServletException e) {
            e.printStackTrace();
        }
        return Const.REDIRECT+"/admin_pro?method=getProByKey";
    }

    /**
     * 获取商品数据  动态查询
     * @param request
     * @param response
     * @return
     */
    public String getProByKey(HttpServletRequest request,HttpServletResponse response){
        //获取动态查询使用的条件参数
        String pname = request.getParameter("pname");
        String ptime = request.getParameter("ptime");
        //访问业务层
        List<Product> proListByKey = productService.getProListByKey(pname,ptime);
        //使用request进行存值
        request.setAttribute("proList",proListByKey);
        return Const.FORWARD+"/admin/showGoods.jsp";
    }

    /**
     * 新增商品类别
     * @param request
     * @param response
     * @return
     */
    public String addType(HttpServletRequest request,HttpServletResponse response) {
        String tname = request.getParameter("tname");
        String tinfo = request.getParameter("tinfo");
        typeService.addType(tname, tinfo);
        return Const.REDIRECT+"/admin_pro?method=getGoodsType";
    }

    /**
     * 获取商品类别数据  动态查询
     * @param request
     * @param response
     * @return
     */
    public String getGoodsType(HttpServletRequest request,HttpServletResponse response) {
        String tname = request.getParameter("tname");
        //向业务层查询
        List<Type> allByKey = typeService.getAllByKey(tname);
        request.setAttribute("goodsTypeList",allByKey);
        String type = request.getParameter("type");
        if(type != null && type.equals("1")){
            return Const.FORWARD+"/admin/showGoodsType.jsp";
        }else {
            return Const.FORWARD+"/admin/addGoods.jsp";
        }
    }
}
